import pandas as pd
import plotly.express as px
from jinja2 import Environment, FileSystemLoader

# 生成示例数据
data = {
    'x': [1, 2, 3, 4, 5],
    'y': [2, 4, 6, 8, 10]
}
df = pd.DataFrame(data)

# 创建 Plotly 图表
fig = px.line(df, x='x', y='y')
plotly_chart = fig.to_json()

# 配置 Jinja2 环境
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('./生成报告/report_tp.html')

# 渲染模板
output = template.render(data=df, plotly_chart=plotly_chart)

# 保存为 HTML 文件
output_file_path = 'report.html'
with open(output_file_path, 'w', encoding='utf-8') as f:
    f.write(output)

print(f"HTML 报告已保存为 {output_file_path}")